//
//  HWTopupStatisticsViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/3/4.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWTopupStatisticsViewController.h"
#import "HWTopupStatisticsModel.h"
#import "HWTopupStatisticsCell.h"


@interface HWTopupStatisticsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) UIView *headerView;
@end

@implementation HWTopupStatisticsViewController

-(UITableView *)mainaTableVie {
    
    if  (_mainaTableVie == nil) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(20, NavBarHeight + 10, kScreenW-40, kScreenH) style:UITableViewStylePlain];
        // _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-44) style:UITableViewStylePlain];
     //   _mainaTableVie.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = beijingyanSe;
        _mainaTableVie.layer.cornerRadius = 12;
        _mainaTableVie.layer.masksToBounds = YES;
        _mainaTableVie.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _mainaTableVie.showsVerticalScrollIndicator = NO;
       // _mainaTableVie.backgroundColor = [UIColor whiteColor];
    }
    
    
    return   _mainaTableVie;
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCommonAttributes];
    
    // 设置UI
    [self setupUI];
    
    [self addheaderView];
    [self addrefreshControl];
     
}


- (void)addheaderView {
    
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 37)];
    CGFloat W = kScreenW - 40;
    CGFloat H = 37;
    CGFloat meigelabeW = W / 5;
    
    NSArray *dataArr = @[@"用户",@"游戏",@"充值",@"实付",@"时间"];
    
    
    for (int i = 0 ; i < 5; i++) {

             UILabel   *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(meigelabeW*i, 0, meigelabeW, H)];
             userLabel.text = dataArr[i];
             userLabel.textAlignment =  NSTextAlignmentCenter;
             userLabel.textColor = RGB(0, 0, 0);
             userLabel.alpha = 0.65;
             userLabel .font = [UIFont systemFontOfSize:12];
             userLabel.hidden = NO;
             userLabel.backgroundColor = RGB(255, 255, 255);
             userLabel.numberOfLines = 2;
             [ self.headerView   addSubview: userLabel];
    
           
       }
    
      for (int i = 0 ; i < 6; i++) {

          UIView *fengexian = [[UIView alloc]initWithFrame:CGRectMake(i*meigelabeW, 0, 1, H)];
          fengexian.backgroundColor = RGB(232, 232, 232);
          [self.headerView addSubview:fengexian];
        
         
          
      }
      UIView *fengexian =  [[UIView alloc]initWithFrame:CGRectMake(0, 36, W, 1)];
      fengexian.backgroundColor = RGB(232, 232, 232);
      [self.headerView addSubview:fengexian];
    
    
      self.mainaTableVie.tableHeaderView  =  self.headerView;
    
    
    
    
    
    
    
}



- (void)addrefreshControl {
   
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}




// 下拉刷新
- (void)underRefresh {
      self.p = @"1";
      self.dataMuArr = [[NSMutableArray alloc]init];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *uid =  [HWLocallyStoredData getLoginUserID];
      NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
      NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
      NSDictionary *parametes = @{      @"ios" : @"1",
                                   @"ct" : @"app",
                                   @"ac" : @"pay",
                                   @"t" : tStr,
                                   @"uid":uid,
                                   @"sign" :signStrMd5,
                                 
                            };
            
            
                [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                      
                       [self.mainaTableVie.mj_header endRefreshing];
                       [self.mainaTableVie.mj_footer endRefreshing];

                      NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                      if ([codeStr intValue] == 1) {
                          
                          
                          self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                          
                          self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                          self.dataMuArr = [HWTopupStatisticsModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                          [self.mainaTableVie reloadData];
                           NSLog(@"%@",response);
                    
                          
                           
                      } else {
                          
                        [MBProgressHUD showError:response[@"message"]];
                                        
                    
                          
                      }
                      
                 
                      
                  } failure:^(NSString *error) {
                     // [SVProgressHUD dismiss];
                      [self.mainaTableVie.mj_header endRefreshing];
                      [self.mainaTableVie.mj_footer endRefreshing];
                      
                      
//                       [MBProgressHUD showSuccess:@"登录失败"];
//                       [MBProgressHUD hideHUD];
                      
                      
                  }];
            
        
    
      
    
    


}





// 上拉加载

- (void)onRefresh {
    
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
         
          [self.mainaTableVie.mj_header endRefreshing];
          [self.mainaTableVie.mj_footer endRefreshing];

        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
    
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *uid =  [HWLocallyStoredData getLoginUserID];
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
          NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
          NSDictionary *parametes = @{    @"ios" : @"1",
                                    @"ct" : @"app",
                                    @"ac" : @"pay",
                                    @"t" : tStr,
                                    @"uid":uid,
                                    @"sign" :signStrMd5,
                                    
                                };
                
                
                    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                          
                           [self.mainaTableVie.mj_header endRefreshing];
                           [self.mainaTableVie.mj_footer endRefreshing];

                          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                          if ([codeStr intValue] == 1) {
                              
                              
                              self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                              self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                              
                              
                              NSMutableArray *mutableArr = [HWTopupStatisticsModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                              
                              
                              for (int i = 0; i < mutableArr.count; i++) {


                                  [self.dataMuArr addObject:mutableArr[i]];

                               }
                              
                              
                              [self.mainaTableVie reloadData];
                         
                                NSLog(@"%@",response);
                              
                    
                              
                               
                          } else {
                              
                            [MBProgressHUD showError:response[@"message"]];
                                            
                        
                              
                          }
                          
                     
                          
                      } failure:^(NSString *error) {
                         // [SVProgressHUD dismiss];
                          [self.mainaTableVie.mj_header endRefreshing];
                          
        
                          [self.mainaTableVie.mj_footer endRefreshing];
                          
                          
                          
                          
    //                       [MBProgressHUD showSuccess:@"登录失败"];
    //                       [MBProgressHUD hideHUD];
                          
                          
                      }];
                
            
    
    
    
        
                          
    
}


















// 设置常用的属性
- (void)setupCommonAttributes {
    
    self.navigationItem.title = @"充值统计";
    self.view.backgroundColor = beijingyanSe;
    
    
    
    
}

// 设置UI
- (void)setupUI {
    
    self.view.backgroundColor = beijingyanSe;
    [self.view addSubview:self.mainaTableVie];
    [self.mainaTableVie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(0);
    }];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return self.dataMuArr.count;
    
    
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    HWTopupStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[HWTopupStatisticsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
   HWTopupStatisticsModel *model =   self.dataMuArr[indexPath.row];
    cell.model = model;
    
     
    return  cell;
    
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  58;
    
    
    
}



@end
